﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using compshop.Modules.GeneralProducts.Presenter;
using compshop.Infrastructure.CompShopDataService;
using System.IO;
using System;

namespace compshop.Modules.GeneralProducts.Views
{
    public partial class GeneralProductsView : UserControl, IViewGeneralProducts
    {
        public GeneralProductsView()
        {
            InitializeComponent();

            GridView grdview = new GridView();
            lstvGeneralProduct.View = grdview;

            GridViewColumn col = new GridViewColumn();
            col.Header = Resource.Name;
            col.DisplayMemberBinding = new Binding("Name");
            col.Width = 150;
            grdview.Columns.Add(col);

            col = new GridViewColumn();
            col.Header = Resource.Producer;
            col.DisplayMemberBinding = new Binding("Producer.Name");
            col.Width = 90;
            grdview.Columns.Add(col);

            col = new GridViewColumn();
            col.Header = Resource.Country;
            col.DisplayMemberBinding = new Binding("Country.Name");
            col.Width = 120;
            grdview.Columns.Add(col);

            col = new GridViewColumn();
            col.Header = Resource.Price;
            col.DisplayMemberBinding = new Binding("CurrentPrice");
            col.Width = 70;
            grdview.Columns.Add(col);

            col = new GridViewColumn();
            col.Header = Resource.Description;
            col.DisplayMemberBinding = new Binding("Info");
            col.Width = 150;
            grdview.Columns.Add(col);

            //col = new GridViewColumn();
            //col.Header = "Фото";
            //col.DisplayMemberBinding = new Binding("Photo");
            //grdview.Columns.Add(col);


        }


        public GeneralProductsPresenter Presenter
        {
            get { return DataContext as GeneralProductsPresenter; }
            set
            {
                DataContext = value;

                lstvGeneralProduct.Items.Clear();
                if (Presenter.GeneralProductList != null)
                {
                    foreach (GeneralProduct gprod in Presenter.GeneralProductList)
                    {
                        lstvGeneralProduct.Items.Add(gprod);
                    }
                }
            }
        }

        public GeneralProductsView(GeneralProductsPresenter presenter)
            : this()
        {
            Presenter = presenter;
            Presenter.View = this;
        }

        #region ButtonHandlers

        private void btnAddToBasket_Click(object sender, RoutedEventArgs e)
        {
            if (lstvGeneralProduct.SelectedItem != null)
            {
                GeneralProduct product = lstvGeneralProduct.SelectedItem as GeneralProduct;
                if (product == null)
                    return;

                if (!Presenter.AddToBasketPublisher((int)product.GeneralProductID))
                {
                    MessageBox.Show(Resource.MsgNoProduct);
                }
            }
        }

        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            if (lstvGeneralProduct.SelectedItem != null)
            {
                GeneralProduct product = lstvGeneralProduct.SelectedItem as GeneralProduct;
                if (product == null)
                    return;

                Presenter.ShowEditGeneralProductDialog(product);
            }
        }

        private void btnAddProduct_Click(object sender, RoutedEventArgs e)
        {
            Presenter.ShowCreateGeneralProductDialog();
        }

        #endregion

        #region ListViewHandlers

        private void lstvGeneralProduct_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lstvGeneralProduct.SelectedItem != null)
            {
                GeneralProduct product = lstvGeneralProduct.SelectedItem as GeneralProduct;
                if (product == null)
                    return;
                Presenter.GeneralProductSelectedPublisher(product.GeneralProductID);
            }
            else
                Presenter.GeneralProductSelectedPublisher(0);
        }

        private void lstvGeneralProduct_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lstvGeneralProduct.SelectedItem != null)
            {
                GeneralProduct product = lstvGeneralProduct.SelectedItem as GeneralProduct;
                if (product.Photo != null && product.Photo.Length > 0)
                {
                    BitmapImage bi = new BitmapImage();
                    MemoryStream ms = new MemoryStream(product.Photo);

                    try
                    {
                        bi.BeginInit();
                        bi.StreamSource = ms;
                        bi.EndInit();
                        string title = string.Format("{0} {1}", product.Producer.Name, product.Name);
                        PhotoWindow win = new PhotoWindow(bi, title);
                        win.Show();
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }

        #endregion

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //cboxProducer.ItemsSource = Presenter.ProducerList;
            //cboxProducer.DisplayMemberPath = "Name";
            //if (cboxProducer.ItemsSource != null && Presenter.ProducerList.Count > 0)
            //    cboxProducer.SelectedIndex = 0;
        }

        private void cboxProducer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //Producer prd = cboxProducer.SelectedItem as Producer;
            //if (prd == null)
            //    return;
            //Presenter.SelectedProducerID = prd.ProducerID;
        }
    }
}
